<?php
namespace Cache;

/** 
 * @brief Redis缓存处理类 
 */
class Redis {
    /** 
     * Redis实例
     *
     * @var object 
     */
    protected $handler;

    /** 
     * 缓存服务器配置参数
     * 
     * @var array 
     */
    private $_options = array();

    /**
     * 构造函数 
     * 
     * @note 打开Redis连接 
     * @param string $name Redis配置名称 
     * 
     * @return void
     */
    public function __construct($name = 'master') {
        if (!extension_loaded('redis')) {
            throw new \Exception('The redis extension must be loaded before use');
        }

        $config = \Yaf_Registry::get('Config');
        $this->_options['host'] = $config->redis->$name->host;
        $this->_options['port'] = $config->redis->$name->port;
        $this->_options['password'] = $config->redis->$name->password;
        $this->_options['pconnect'] = $config->redis->$name->pconnect;
        $this->_options['timeout'] = $config->redis->$name->timeout;
        $this->_options['select'] = $config->redis->$name->select;
        $this->_options['expire'] = $config->redis->$name->expire;

        $this->handler = new \Redis;
        if ($this->_options['pconnect']) {
            $this->handler->pconnect($this->_options['host'], $this->_options['port'], $this->_options['timeout'], 'pid_' . $this->_options['select']);
        } else {
            $this->handler->connect($this->_options['host'], $this->_options['port'], $this->_options['timeout']);
        }
        if ('' != $this->_options['password']) {
            $this->handler->auth($this->_options['password']);
        }
        if (0 != $this->_options['select']) {
            $this->handler->select($this->_options['select']);
        }
    }

    /**
     * 写入缓存 
     * 
     * @param string $name 缓存变量名
     * @param mixed $value 数据
     * @param int $expire 有效时间(单位为秒) 
     * 
     * @return boolean
     */
    public function set($name, $value, $expire = null) {
        $expire = ($expire === null) ? $this->_options['expire'] : (int) $expire;
        if (0 == $expire) {
            return $this->handler->set($name, $value);
        } else {
            return $this->handler->setex($name, $expire, $value);
        }
    }

    /**
     * 读取缓存 
     * 
     * @param string $name 缓存变量名
     * @param mixed $default 默认值
     * 
     * @return mixed
     */
    public function get($name, $default = null) {
        $value = $this->handler->get($name);
        return (false !== $value) ? $value : $default;
    }

    /**
     * 自增缓存（针对数值缓存）
     * 
     * @param string $name 缓存变量名
     * @param int    $step 步长
     * @return false|int
     */
    public function increment($name, $step = 1) {
        return $this->handler->incrBy($name, $step);
    }

    /**
     * 自减缓存（针对数值缓存）
     * 
     * @param string $name 缓存变量名
     * @param int    $step 步长
     * @return false|int
     */
    public function decrement($name, $step = 1) {
        return $this->handler->decrBy($name, $step);
    }

    /**
     * 删除指定的缓存 
     * 
     * @param array|string $key 缓存变量名
     * 
     * @return boolean
     */
    public function delete($key) {
        return $this->handler->unlink($key);
    }

    /**
     * 清空当前DB的缓存
     * 
     * @return boolean
     */
    public function flush() {
        return $this->handler->flushDB();
    }

    /**
     * 扫描缓存 
     * 
     * @param mixed $iterator 迭代器
     * @param string $pattern 匹配格式
     * 
     * @return mixed
     */
    public function scan($iterator, $pattern) {
        return $this->handler->scan($iterator, $pattern);
    }
    
    /**
     * 关闭Redis连接 
     * 
     * @return boolean
     */
    public function close() {
        return $this->handler->close();
    }
}
